package com.ost.kautilya.modeling.weka;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.management.AttributeNotFoundException;

import weka.core.Option;

import com.ost.kautilya.modeling.ModelConfigurationParameter;

public class WekaOption extends ModelConfigurationParameter {

	public WekaOption(int index, Option opt, List<String> ao) throws AttributeNotFoundException {
		super(index, opt.name(), opt.description());
		Pattern pattern = Pattern.compile("-[A-Za-z]");
		int oi = ao.indexOf("-" + opt.name());
		if (oi < 0) {
			int oc = opt.numArguments();
			if ("".equals(getName()) && "".equals(getDescription())) {
				throw new AttributeNotFoundException(opt.synopsis());
			}
			switch(oc) {
			case 0:
				isf = true;
				dval = String.valueOf(false);
				break;
			case 1:
				int ii = ao.indexOf("-" + getName());
				if (ii >= 0  && ii + 1 <= ao.size()) {
					dval = ao.get(ii + 1);
					on = true;
				} else {
					on  = false;
				}
				break;
			default :
				break;
			}
		} else if (oi < ao.size() - 1) {
			int ni = oi + 1;
			String no = ao.get(ni);
			Matcher matcher = pattern.matcher(no);
			if (matcher.matches()) {
				isf = true;
				dval = String.valueOf(isf);
			} else {
				dval = no;
			}
		} else {
			isf = true;
			dval = String.valueOf(isf);
		}
		on = dval != null;
	}

}
